package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kashyapk on 24/02/14.
 */
public class Library {
    private List<Book> books = new ArrayList<Book>();
    private List<Customer> customers = new ArrayList<Customer>();
    private List<Movie> movies = new ArrayList<Movie>();

    public Library() {
        customers.add(new Customer("bruce","wayne", "batman@gotham.com", "911"));

        books.add(new Book("1","Fellowship of the ring"));
        books.add(new Book("2","The two towers"));
        books.add(new Book("3","Book of the king"));

        movies.add(new Movie("A New Hope", "Lucas", 1977, 10));
        movies.add(new Movie("The Empire Strikes Back", "Lucas", 1980, 10));
        movies.add(new Movie("Return of the Jedi", "Lucas", 1983, 10));

    }

    public List<Book> ListBooks() {
        return books;
    }

    public String CheckoutBook(String bookTitle, Customer customer) {
        for(Book book : books) {
            if(book.GetTitle().equals(bookTitle)) {
                customer.CheckoutBook(book);
                books.remove(book);
                return("Thank you! Enjoy the book");
            }
        }
        return("That book is not available");
    }

    public String ReturnBook(String bookTitle, Customer customer) {
        Book book = customer.ReturnBook(bookTitle);
            if(book != null){
                books.add(book);
                return "Thank you for returning the book";
            }
        return "That is not a valid book to return";
    }

    public List<Movie> ListMovies() {
        return movies;
    }

    public String CheckoutMovie(String movieTitle, Customer customer) {
        for(Movie movie : movies) {
            if(movie.getName().equals(movieTitle)) {
                customer.CheckoutMovie(movie);
                movies.remove(movie);
                return("Thank you! Enjoy the movie");
            }
        }
        return("That movie is not available");
    }

    public String ReturnMovie(String movieTitle, Customer customer) {
        Movie movie = customer.ReturnMovie(movieTitle);
        if(movie != null) {
            movies.add(movie);
            return "Thank you for returning the movie";
        }
        return "That is not a valid movie to return";
    }

    public Customer UserLogin(String username, String password) {
        for(Customer customer : customers) {
            if(username.equals(customer.getName()) && password.equals(customer.getPassword())){
                return customer;
            }
        }
        return null;
    }
}